//
//  HSSFFinancialViewController.m
//  HSSFinancial
//
//  Created by xinqiao on 2017/1/10.
//  Copyright © 2017年 Jon_G. All rights reserved.
//

#import "HSSFFinancialViewController.h"

#import "FinancialHuiPlanTableViewCell.h"
#import "FinanciaInstallmentTableViewCell.h"

#import "FinancialListViewController.h"
#import "FinancialInvestListViewController.h"

#import "HuisangsuoApi.h"
#import "promptAlertViewController.h"
#import "MBProgressHUD.h"
#import "G_Color.h"

@interface HSSFFinancialViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) NSDictionary *financialDic;

@end

@implementation HSSFFinancialViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"理财";
    [self getFinancialDate];
    [self getFinancialDataSourceToSetUI];
}
// 请求
- (void)getFinancialDate {
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    self.hud.bezelView.color = [UIColor clearColor];
    [HuisangsuoApi requestgetDate:MainManageMoney dic:nil AndBack:^(id obj) {
        [self.hud hideAnimated:YES];
        if (obj != nil) {
            if ([obj[@"status"] integerValue] == 1) {
                self.financialDic = obj;
                [self.financialTableView reloadData];
            }else {
                [promptAlertViewController showButtonAlertView:self withMessage:obj[@"message"] actionMessage:@"确定"];
            }
        }
    } error:^(id obj) {
        [self.hud hideAnimated:YES];
        [promptAlertViewController showButtonAlertView:self withMessage:@"请求错误，稍后重试！" actionMessage:@"确定"];
    }];
}
#pragma mark - UITableView
- (void)getFinancialDataSourceToSetUI {
    self.financialTableView.dataSource = self;
    self.financialTableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10;
    }
    return 61;
}
// 返回section的头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section != 0) {
        return [self addSectionHeader:section];
    }
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 10)];
    sectionView.backgroundColor = [G_Color colorWithNSString:@"efeff4" alpha:1.0];
    return sectionView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 76;
    }
    return 151;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (indexPath.section == 0) {
        FinancialHuiPlanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"FinancialHuiPlanTableViewCell" owner:self options:nil]lastObject];
            
            if (indexPath.section == 0) {
                if (indexPath.row == 0) {
                    cell.namelabel.text = @"汇计划";
                    cell.huiPlanImageView.image = [UIImage imageNamed:@"financial_hui"];
                    cell.biaoYuLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"fundtitle"]];
                    if ([self.financialDic[@"fundnum"] integerValue] == 0) {
                        cell.numberLabel.hidden = YES;
                    }
                    cell.numberLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"fundnum"]];
                }
                if (indexPath.row == 1) {
                    cell.namelabel.text = @"散标投资";
                    cell.huiPlanImageView.image = [UIImage imageNamed:@"financial_San"];
                    cell.biaoYuLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"investtitle"]];
                    if ([self.financialDic[@"investnum"] integerValue] == 0) {
                        cell.numberLabel.hidden = YES;
                    }
                    cell.numberLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"investnum"]];
                }
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            return cell;
        }
    }else {
        FinanciaInstallmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"FinanciaInstallmentTableViewCell" owner:self options:nil]lastObject];
            // 汇计划
            if (indexPath.section == 1) {
                cell.titleNameLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"funddata"][indexPath.row][@"borrow_name"]];
                cell.borrow_basis_rateLabel.text = [NSString stringWithFormat:@"%@%%",self.financialDic[@"funddata"][indexPath.row][@"borrow_basis_rate"]];
                cell.borrow_durationLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"funddata"][indexPath.row][@"borrow_duration"]];
                cell.transfer_total_Label.text = [NSString stringWithFormat:@"%@份",self.financialDic[@"funddata"][indexPath.row][@"transfer_total"]];
                cell.progress_View.progress = [self.financialDic[@"funddata"][indexPath.row][@"progress"] integerValue] * 0.01;
                cell.per_transfer_Label.text = [NSString stringWithFormat:@"%@元起",self.financialDic[@"funddata"][indexPath.row][@"per_transfer"]];
                cell.need_transfer_Label.text = [NSString stringWithFormat:@"剩余份数：%@份",self.financialDic[@"funddata"][indexPath.row][@"need_transfer"]];
            }
            // 散标投资
            if (indexPath.section == 2) {
                cell.total_name_Label.text = @"总金额";
                cell.titleNameLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"investdata"][indexPath.row][@"borrow_name"]];
                cell.borrow_basis_rateLabel.text = [NSString stringWithFormat:@"%@%%",self.financialDic[@"investdata"][indexPath.row][@"borrow_interest_rate"]];
                cell.borrow_durationLabel.text = [NSString stringWithFormat:@"%@",self.financialDic[@"investdata"][indexPath.row][@"borrow_duration"]];
                cell.transfer_total_Label.text = [NSString stringWithFormat:@"%@万元",self.financialDic[@"investdata"][indexPath.row][@"borrow_money"]];
                cell.progress_View.progress = [self.financialDic[@"investdata"][indexPath.row][@"progress"] integerValue] * 0.01;
                cell.per_transfer_Label.text = [NSString stringWithFormat:@"%@元起",self.financialDic[@"investdata"][indexPath.row][@"borrow_min"]];
                cell.need_transfer_Label.text = [NSString stringWithFormat:@"剩余金额：%@万元",self.financialDic[@"investdata"][indexPath.row][@"need_borrow"]];
            }
            
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            return cell;
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            FinancialListViewController *listVC = [[FinancialListViewController alloc] init];
            listVC.title_name = @"汇计划";
            [self.navigationController pushViewController:listVC animated:YES];
        }else {
            FinancialInvestListViewController *listVC = [[FinancialInvestListViewController alloc] init];
            listVC.title_name = @"散标投资";
            [self.navigationController pushViewController:listVC animated:YES];
        }
    }
}
#pragma mark - SectionHeader
- (UIView *)addSectionHeader:(NSInteger)section {
    //背景view
    UIView *sectionBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 61)];
    sectionBackGroundView.backgroundColor = [G_Color colorWithNSString:@"efeff4" alpha:1.0];
    // 白色view
    UIView *weitBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 50)];
    weitBackGroundView.backgroundColor = [UIColor whiteColor];
    [sectionBackGroundView addSubview:weitBackGroundView];
    // name
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 19, 70, 14)];
    if (section == 1) {
        nameLabel.text = @"汇计划";
    }else {
        nameLabel.text = @"散标投资";
    }
    nameLabel.font = [UIFont systemFontOfSize:16.0];
    nameLabel.textColor = [G_Color colorWithNSString:@"333333" alpha:1.0];
    [weitBackGroundView addSubview:nameLabel];
    // 更多
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton setTitle:@"更多" forState:UIControlStateNormal];
    [moreButton setTitleColor:[G_Color colorWithNSString:@"666666" alpha:1.0] forState:UIControlStateNormal];
    moreButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    moreButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 67, 5, 40, 40);
    moreButton.tag = section;
    [moreButton addTarget:self action:@selector(addMoreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [weitBackGroundView addSubview:moreButton];
    // 箭头
    UIImageView *jiantouImageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 23, 16.5, 8, 13)];
    jiantouImageView.image = [UIImage imageNamed:@"financial_Fan"];
    [weitBackGroundView addSubview:jiantouImageView];
    return sectionBackGroundView;
}
- (void)addMoreButtonAction:(UIButton *)button {
    
//    FinancialListViewController *listVC = [[FinancialListViewController alloc] init];
//    if (button.tag == 1) {
//        listVC.title_name = @"";
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
